package com.example.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.example.entity.Book;
import com.example.entity.Car;
import com.example.entity.User;
import com.example.service.BookService;
import com.example.service.CarService;
import com.example.service.UserService;
import com.example.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/bookshop")
public class IndexController {

    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;
    @Autowired
    CarService carService;

    /**
     * 展示首页页面
     */
    @RequestMapping("/indexPage")
    public String indexPage(){
        return "index";
    }

    /**
     * 获取所有图书信息
     */
    @ResponseBody
    @RequestMapping(value="/getBookData", produces="application/json; charset=utf-8")
    public String getBookData(@RequestBody Book book){
        List<Book> bookList  = bookService.findList(book);
        Integer listCount = bookService.findAllCount(book);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", bookList);
        map.put("count", listCount);
        return JSON.toJSONString(map);
    }

    /**
     * 根据id获取图书信息
     */
    @ResponseBody
    @RequestMapping(value="/getBookById")
    public String getBookById(String id){
        Book b = bookService.get(id);
        return JSON.toJSONString(b);
    }

    /**
     * 加入购物车
     */
    @ResponseBody
    @RequestMapping(value="/saveCar")
    public String saveCar(@RequestBody Car car){
        if(StrUtil.isEmpty(car.getId())){
            //如果是新增
            User currentUser = (User) UserContext.getCurrentUser();
            car.setUserId(currentUser.getId());
            car.setBookNumber("1");
            car.setOrderFlag("0");
        }
        carService.save(car);
        return "success";
    }

    /**
     * 购物车页面
     */
    @RequestMapping("/carPage")
    public String carPage(){
        return "car";
    }

    /**
     * 获取自己的购物车信息
     */
    @ResponseBody
    @RequestMapping(value="/getCarData", produces="application/json; charset=utf-8")
    public String getCarData(@RequestBody Car car){
        //只查询自己的
        User currentUser = (User) UserContext.getCurrentUser();
        car.setUserId(currentUser.getId());
        //数据封装
        List<Car> carList  = carService.findList(car);
        Integer listCount = carService.findAllCount(car);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", carList);
        map.put("count", listCount);
        return JSON.toJSONString(map);
    }

    /**
     * 删除购物车
     * @param id  购物车唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/deleteCar")
    public String deleteCar(String id){
        carService.delete(id);
        return "success";
    }

    /**
     * 加一本
     * @param id  购物车唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/addOne")
    public String addOne(String id){
        Car car = carService.get(id);
        car.setBookNumber((Integer.parseInt(car.getBookNumber()) + 1)+"");
        carService.save(car);
        return "success";
    }

    /**
     * 减一本
     * @param id  购物车唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/reduceOne")
    public String reduceOne(String id){
        Car car = carService.get(id);
        if(car.getBookNumber().equals("1")){
            carService.delete(id);
        }else{
            car.setBookNumber((Integer.parseInt(car.getBookNumber()) - 1)+"");
            carService.save(car);
        }
        return "success";
    }

    /**
     * 减一本
     * @param id  购物车唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/pay")
    public String pay(String id, String bookId){
        Car car = carService.get(id);
        if(car.getOrderFlag().equals("1")){
            return "finish";
        }else{
            Book b = bookService.get(bookId);
            Integer bookNum = Integer.parseInt(b.getBookNumber());
            Integer needNum = Integer.parseInt(car.getBookNumber());
            if(needNum > bookNum){
                return "false";
            }
            b.setBookNumber(String.valueOf(bookNum - needNum));
            bookService.save(b);
            car.setOrderFlag("1");
            carService.save(car);
            return "success";
        }
    }



}
